const app = getApp();
function request(success) {
	let that=this;
	//let userInfo = wx.getStorageSync('userInfo');
	let appurl = app.globalData.baseurl + this.url;
	let contenttype = '';
	if (this.methods == 'post'&&this.nonelist===undefined) {
		contenttype = 'application/json';
	} else if(this.methods == 'get'){
		contenttype = 'application/json';
	}else if(this.methods == 'post'&&this.nonelist===true){
		contenttype = 'application/x-www-form-urlencoded;charset=utf-8';
	};
	if(this.loading===undefined){
		console.log('loading');
		wx.showLoading({
			title: '客官不要急~',
			mask: true
		});
	};
	console.log(this.methods);
	wx.request({
		url: appurl,
		header: {
			'content-type': contenttype
			/* 'headerId': userInfo.openId,
			'headerValue': userInfo.headerValue */
		},
		method: this.methods,
		data: this.data,
		success(res) {
			if (res.status == 200) {
				if(that.loading===undefined){
					wx.hideLoading(); /* 隐藏加载框 */
				};
				success(res);
			} else if(res.status == 500){
				if(that.loading===undefined){
					wx.hideLoading(); /* 隐藏加载框 */
				};
				success(res);
				wx.showModal({
					title: "提示",
					content: res.data.msg,
					showCancel: false
				});
			}else{
				if(that.loading===undefined){
					wx.hideLoading(); /* 隐藏加载框 */
				};
				success(res);
			};
		},
		fail(res){
			if(that.loading===undefined){
				wx.hideLoading(); /* 隐藏加载框 */
			};
			wx.showLoading({
				title: '请检查网络重启~',
				mask: true
			});
		}
	});
};

function _login(_success) {
	let that = this;
	//let loginnum=num;
	// 登录
	if(this.loading===undefined){
		wx.showLoading({
			title: '客官不要急~',
			mask: true
		});
	};
	wx.login({
		success: res => {
			if(res.errMsg=='login:ok'){
				let usercode = res.code;
				wx.setStorageSync('usercode', usercode.code);
				let base_url = app.globalData.baseurl;
				let _url = base_url + '/applet/wechat/login';
				wx.request({
					url: _url,
					data: {
						code: usercode,
						deviceCode:this.deviceCode
					},
					method: 'post',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
					},
					success: (res)=> {
						_success(res);
						if(this.loading===undefined){
							wx.hideLoading(); /* 隐藏加载框 */
						};
					},
				})
			}},
		fail: res => {
			if(this.loading===undefined){
				wx.hideLoading(); /* 隐藏加载框 */
			};
			wx.showToast({
				'title':'微信登录失败'
			})
		}
	});
};
function time(time){
	return  time.substring(0, 4) + "-" + time.substring(4, 6) + "-" +
			time.substring(6, 8) + "  " + time.substring(8, 10) + ":" + time.substring(
				10, 12) + ":" + time.substring(12, 14)
	
};
function tips(title){
	wx.showToast({title: title,icon: 'none'});
};
function confirm(content,success){
	wx.showModal({
		title: "提示",
		content: content,
		showCancel: true,
		success: function(res) {
			if (res.confirm) {
				/*确认收货函数*/
				console.log('确定');
				success()
			} else if (res.cancel) {
				console.log('取消');
				return false;
			}
		}
	});
};
function date(value){
	var date = new Date(value*1000) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-'
	var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-'
	var D = date.getDate() + ' '
	var h = date.getHours() + ':'
	var m = date.getMinutes();
	var s = date.getSeconds();
	if(m==0){
		m='00'
	};
	return Y+M+D+h+m
};
function day(value){
	var date = new Date(value) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var D = date.getDate();
	return D;
};
module.exports = {
	request,
	_login,
	time,
	tips,
	confirm,
	date,
	day,
}
